#!/usr/bin/python

DEBUG_ = False
#DEBUG_ = True

def log(str):
	if DEBUG_:
		print str

if __name__ == '__main__':
	import os
	import sys
	import string
	import subprocess
	import getopt

VERSION = (1, 0)
GIT = 'git'
HELP = "        There is not a branch in this dir, please check!"

def get_branch_count():
	cmd = [GIT, 'branch']
	proc = subprocess.Popen(cmd, stdout=subprocess.PIPE)
	branch_count = 0
	for branch in proc.stdout.readlines():
		if -1==string.find(branch, 'fatal:'):
			log( "one line: %s" %branch )
			branch_count = branch_count + 1
		else:
			return 0
	
	log( "branch_count : %s" %branch_count )

	proc.stdout.close()
	proc.wait()
	return branch_count

def show_branch( all=None ):
	cmd = [GIT, 'show-branch']
	proc = subprocess.Popen(cmd, stdout=subprocess.PIPE)

	if not all:
		log("show top")
		for line in proc.stdout.readlines():
			line = string.replace(line, "\n", "")
			if split_symbol!=line:
				print line
			else:
				break
	elif True==all:
		log("show all")
		for line in proc.stdout.readlines():
			line = string.replace(line, "\n", "")
			print line
	
	proc.stdout.close()
	proc.wait()

def parse_arguments(sys_args):
	log('input args : %s' %sys_args)
	opt, args = getopt.getopt(sys_args, 'a')

	log('opt : %s ' %opt)
	global show_all
	show_all = False
	for o,v in opt:
		if '-a'==o:
			log('set show_all = True')
			show_all = True

def main(orgin_args):
	count = get_branch_count()
	if 0==count:
		print HELP
		sys.exit(1)

	global split_symbol
	split_symbol = ""
	for i in range(count):
		split_symbol = split_symbol + "-"
	log("split_symbol : %s" %split_symbol)

	parse_arguments(orgin_args)
	log('show_all : %s' %show_all)
	show_branch(show_all)

if __name__ == '__main__':
	main(sys.argv[1:])
